//
//  EaseLoginManager.m
//  ChatDemo
//
//  Created by 度周末网络-王腾 on 15/12/21.
//  Copyright © 2015年 imac. All rights reserved.
//

#import "EaseLoginManager.h"
#import "AppDelegate.h"
@implementation EaseLoginManager

+ (instancetype)shareManager{
    static EaseLoginManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];
    });
    return manager;
}

/**
 *  开始监听登陆状态
 */
-(void)startListenLoginStatus{
    
    //注册登录状态监听
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loginStateChange:)
                                                 name:KNOTIFICATION_LOGINCHANGE
                                               object:nil];
    
    
    [self loginStateChange:nil];
    [self registerEaseMobNotification];
}

#pragma mark - login changed
- (void)loginStateChange:(NSNotification *)notification
{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    BOOL isAutoLogin = [[[EaseMob sharedInstance] chatManager] isAutoLoginEnabled];
    BOOL loginSuccess = [notification.object boolValue];
    // 登陆成功加载主窗口控制器
    if (isAutoLogin || loginSuccess) {
   
        //加载申请通知的数据
        [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINSUCCESS object:nil];
        
        if (appDelegate.mainController == nil) {
            appDelegate.mainController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]  instantiateInitialViewController];
        }
    
        appDelegate.window.rootViewController = appDelegate.mainController;
      
    }
    else{
 
        UINavigationController *loginNaviController = [[UIStoryboard storyboardWithName:@"Login" bundle:nil]  instantiateInitialViewController];
        appDelegate.window.rootViewController = loginNaviController;
       
    }
}

#pragma mark - IChatManagerDelegate 设备在其他地方登录
/**
 *  设备在其他地方登录
 */
- (void)didLoginFromOtherDevice
{
    [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:NO completion:^(NSDictionary *info, EMError *error) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"prompt", @"Prompt") message:NSLocalizedString(@"loginAtOtherDevice", @"your login account has been in other places") delegate:self cancelButtonTitle:NSLocalizedString(@"ok", @"OK") otherButtonTitles:nil, nil];
        alertView.tag = 100;
        [alertView show];
    } onQueue:nil];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 99) {
        if (buttonIndex != [alertView cancelButtonIndex]) {
            [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES completion:^(NSDictionary *info, EMError *error) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
            } onQueue:nil];
        }
    }
    else if (alertView.tag == 100) {
        [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
    } else if (alertView.tag == 101) {
        [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
    }
}


#pragma mark - registerEaseMobNotification
- (void)registerEaseMobNotification{
    [self unRegisterEaseMobNotification];
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
}

- (void)unRegisterEaseMobNotification{
    [[EaseMob sharedInstance].chatManager removeDelegate:self];
}

@end
